package com.java.controller;

import com.java.entity.param.WeChatAddDraftParam;
import com.java.entity.vo.Article;
import com.java.entity.vo.WeChatAddDraftRequest;
import com.java.entity.vo.WeChatAddDraftResponse;
import com.java.service.WeChatDraftService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/wechat/draft")
public class WeChatDraftController {

    @Autowired
    private WeChatDraftService draftService;

    /**
     * 新增微信草稿
     *
     * @param accessToken access_token 接口凭证，推荐走服务端自动刷新逻辑
     * @param param       业务参数，articles列表
     */
    @PostMapping("/add")
    public String addDraft(
            @RequestParam("access_token") String accessToken,
            @RequestBody WeChatAddDraftParam param) {
        WeChatAddDraftRequest articles = new WeChatAddDraftRequest();
        Article article = new Article();
        BeanUtils.copyProperties(param, article);
        articles.setArticles(Collections.singletonList(article));
        return draftService.addDraft(accessToken, articles).getMedia_id();
    }
}